package com.wanji.sdk.impl.http;

import com.wanji.protocol.api.ByteBufEncoder;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.*;

public class HttpMessageEncoder extends ByteBufEncoder<FullHttpResponse> {
    @Override
    protected FullHttpResponse toEncodedMessage(ChannelHandlerContext ctx, ByteBuf msg) {
        // 创建 HTTP 响应对象
        DefaultFullHttpResponse response = new DefaultFullHttpResponse(
                HttpVersion.HTTP_1_1,
                HttpResponseStatus.OK,
                msg.retain()
        );

        response.headers()
                .set(HttpHeaderNames.CONTENT_TYPE, "application/json")
                .set(HttpHeaderNames.CONTENT_LENGTH, msg.readableBytes());
        return response;
    }
}
